package com.ysucode.service.oj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ysucode.dao.oj.ProblemMapper;
import com.ysucode.domain.Problem;
import com.ysucode.service.oj.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProblemServiceImpl implements ProblemService {
    @Autowired
    private ProblemMapper problemMapper;

    @Override
    public PageInfo<Problem> getAll(int pageNum, int pageSize) {
        PageInfo<Problem> pageInfo = null;
        //设置分页插件需要显示的页码和每页显示的条数
        PageHelper.startPage(pageNum, pageSize);
        //查询所有的用户信息，自动过滤sql，添加limit
        List<Problem> list = problemMapper.getAll();
        pageInfo = new PageInfo<>(list, pageSize);
        return pageInfo;
    }

    @Override
    public Problem getJavaById(int id) {
        return problemMapper.getJavaById(id);
    }

    @Override
    public Integer addJava(Problem problem) {
        int flag1 = problemMapper.addProblem(problem);
        Integer id = problemMapper.getLastId();
        problem.setId(id);
        int flag2 = problemMapper.addJavaProblem(problem);
        return flag1 == 1 && flag1 == flag2 ? 1 : 0;
    }

}
